//
//  DiZhiXuanZeViewController.m
//  XiangMu
//
//  Created by 康世标 on 15/9/2.
//  Copyright (c) 2015年 康世标. All rights reserved.
//

#import "DiZhiXuanZeViewController.h"

@interface DiZhiXuanZeViewController ()

@end

@implementation DiZhiXuanZeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.title = @"地址选择";
    [self.tableView1 registerNib:[UINib nibWithNibName:@"DiZhiTableViewCell" bundle:nil] forCellReuseIdentifier:@"dizhi"];

    self.button.backgroundColor = CL_00cc85;
    
    self.tableView1.backgroundColor = CL_efeff4;
    
    self.tableView1.separatorColor = CL_efeff4;
    
    [self getWoDeDiZhi];
    
    hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    hud.labelText = @"数据加载中...";
    
}
//获取历史地址
- (void)getWoDeDiZhi{
    
    long ID =[[[NSUserDefaults standardUserDefaults]objectForKey:@"ID"] longValue];
    
    self.diZhiArr = [[NSMutableArray alloc]initWithCapacity:0];
    
    [QingQiuShuJu getWoDeDiZhi:@(ID) BLokc:^(NSDictionary *dic, BOOL isOK)
    {
        
        //        NSLog(@"---%@",dic);
        if (isOK) {
            
            if ([[dic valueForKey:@"status"] intValue]==200)
            {
                
                if ([[dic valueForKey:@"data"]isKindOfClass:[NSNull class]])
                {
                 
                    
                }
                else
                {
                    [self.diZhiArr addObjectsFromArray:[dic valueForKey:@"data"]];
                    
                    [hud setHidden:YES];
                }
               [self.tableView1 reloadData];
            }
            else
            {
                [self.diZhiArr removeAllObjects];
                hud.mode = MBProgressHUDModeText;
                hud.labelText = [dic valueForKey:@"msg"];
               
                if ([[dic valueForKey:@"status"] intValue]==201) {
                    
                    [self addar];
                    
                }
                [hud hide:YES afterDelay:1.5];
                [self.tableView1 reloadData];
            }
        }else{
            hud.mode = MBProgressHUDModeText;
            hud.labelText = @"网络连接失败";
            [hud hide:YES afterDelay:1.5];

        }
        
    }];
}

#pragma mark---UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.diZhiArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DiZhiTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"dizhi" forIndexPath:indexPath];
    
    NSDictionary * dic =[self.diZhiArr objectAtIndex:indexPath.row];

    cell.name.text =[NSString stringWithFormat:@"%@      %@  %@",
                     [dic valueForKey:@"name"]==[NSNull null]?@"":[dic valueForKey:@"name"],
                     [dic valueForKey:@"sex"]==[NSNull null]?@"":[dic valueForKey:@"sex"],
                     [dic valueForKey:@"age"]==[NSNull null]?@"":[dic valueForKey:@"age"]];
    
    cell.tel.text = [dic valueForKey:@"tel"]==[NSNull null]?@"":[dic valueForKey:@"tel"];
    
    cell.diZhi.text =[dic valueForKey:@"addr"] == [NSNull null]?@"":[dic valueForKey:@"addr"];
    
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 69.0f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     NSDictionary * dic =[self.diZhiArr objectAtIndex:indexPath.row];
    self.index (dic);
    
    [self.navigationController popViewControllerAnimated:YES];
    
    

}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary * dic =[self.diZhiArr objectAtIndex:indexPath.row];
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        
        hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        
        [QingQiuShuJu deleteWithDiZhi:@{@"userAddrId":@([[dic valueForKey:@"id"] intValue])} blcok:^(NSDictionary *dic, BOOL isOK)
         {
             if (isOK) {
                 
//                 NSLog(@"---%@",dic);
                 if ([[dic valueForKey:@"status"] intValue]==200) {
                      [self getWoDeDiZhi];
                     
                     hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
                     
                     
                     hud.mode = MBProgressHUDModeCustomView;
                     
                     hud.labelText = @"删除成功";
                     
                    [hud hide:YES afterDelay:2];
                 }
                 else{
                     hud.mode = MBProgressHUDModeText;
                     
                     hud.labelText = [dic valueForKey:@"msg"];
                     
                     [hud hide:YES afterDelay:1.5];

                 }
                 
             }
             else{
                 hud.mode = MBProgressHUDModeText;
                 
                 hud.labelText = @"网络错误";
                 
                 [hud hide:YES afterDelay:1.5];

             }
             
             hud.removeFromSuperViewOnHide = YES;
           
       }];
        
        
    }
}
- (IBAction)XinZeDiZhi:(UIButton *)sender{
    
    
    XinZeDiZhiViewController * x = [[XinZeDiZhiViewController alloc]init];
    
    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:x];
    
    x.block1 = ^(NSDictionary * dic){
        
        [self.diZhiArr addObject:dic];
        
        [self.tableView1 reloadData];
        
        
    };
    
    [self presentViewController:nav animated:YES completion:nil];

    
}
- (void)addar{
    
    XinZeDiZhiViewController * x = [[XinZeDiZhiViewController alloc]init];
    
    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:x];
    
    x.block1 = ^(NSDictionary * dic){
        
        [self.diZhiArr addObject:dic];
        
        [self.tableView1 reloadData];
        
        
    };
    
    [self presentViewController:nav animated:YES completion:nil];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
 
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
